using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.Edm;
using OData.Data;
using OData.Shared.Models;

namespace OData.MinimalApi.Endpoints.Customers
{
	public class GetByKey : IOdataEndpoint
	{
		public void MapEndpoint(IEndpointRouteBuilder app, IEdmModel model)
		{
			app.MapGet("api/customers/{key}", (ApplicationDbContext db, int key, ODataQueryOptions<Customer> options) =>
				{
					db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
					return options.ApplyTo(
						db.Customers.Where(x => x.Id == key),new ODataQuerySettings());
				})
				.WithODataResult()
				.WithODataModel(model)
				.WithTags(Tags.Customers);

		}
	}
}
